<template>
  <el-card class="section-card">
    <template #header>
      <div class="section-header">
        <h3>我的投资</h3>
        <el-button :icon="Refresh" @click="onRefresh" :loading="loading">刷新</el-button>
      </div>
    </template>

    <div class="investments-list" v-loading="loading">
      <div class="investment-item" v-for="investment in investments" :key="investment.id">
        <div class="investment-header">
          <div class="investment-title">{{ investment.productName }}</div>
          <div class="investment-status">
            <el-tag :type="getOrderStatusType(investment.status)">
              {{ getOrderStatusText(investment.status) }}
            </el-tag>
          </div>
        </div>
        <div class="investment-details">
          <div class="detail-row">
            <span class="detail-label">投资金额：</span>
            <span class="detail-value">¥{{ formatAmount(investment.amount) }}</span>
          </div>
          <div class="detail-row">
            <span class="detail-label">当前价值：</span>
            <span class="detail-value">¥{{ formatAmount(investment.currentValue) }}</span>
          </div>
          <div class="detail-row">
            <span class="detail-label">盈亏金额：</span>
            <span class="detail-value" :class="investment.profit >= 0 ? 'positive' : 'negative'">
              {{ formatAmount(investment.profit, true) }}
            </span>
          </div>
          <div class="detail-row">
            <span class="detail-label">投资日期：</span>
            <span class="detail-value">{{ formatDate(investment.createTime) }}</span>
          </div>
        </div>
        <div class="investment-actions">
          <el-button size="small" @click="onInvestmentDetail(investment)">查看详情</el-button>
        </div>
      </div>

      <!-- 分页 -->
      <div class="pagination-wrapper">
        <el-pagination
        :current-page="pagination.page"
        :page-size="pagination.size"
        :page-sizes="[10, 20, 50]"
        :total="pagination.total"
        layout="total, sizes, prev, pager, next"
        size="small"
        @current-change="onPageChange"
        @size-change="onSizeChange"
        />
      </div>
    </div>
  </el-card>
</template>

<script setup>
import { Refresh } from '@element-plus/icons-vue';
import { formatAmount, formatDate, getOrderStatusText, getOrderStatusType } from './utils';

defineProps({
  investments: {
    type: Array,
    required: true
  },
  loading: {
    type: Boolean,
    default: false
  },
  pagination: {
    type: Object,
    required: true
  }
});

const emit = defineEmits(['refresh', 'page-change', 'size-change', 'investment-detail']);

const onRefresh = () => {
  emit('refresh');
};

const onPageChange = (page) => {
  emit('page-change', page);
};

const onSizeChange = (size) => {
  emit('size-change', size);
};

const onInvestmentDetail = (investment) => {
  emit('investment-detail', investment);
};
</script>

<style scoped>
.section-card {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.section-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

/* 投资列表样式 */
.investments-list {
  min-height: 400px;
}

.investment-item {
  border: 1px solid #ebeef5;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 12px;
  background-color: white;
  transition: all 0.3s ease;
}

.investment-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
}

.investment-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.investment-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}

.investment-details {
  margin-bottom: 12px;
}

.detail-row {
  display: flex;
  justify-content: space-between;
  margin-bottom: 6px;
  font-size: 14px;
}

.detail-label {
  color: #909399;
}

.detail-value {
  color: #303133;
  font-weight: 500;
}

.positive {
  color: #67c23a;
}

.negative {
  color: #f56c6c;
}

.investment-actions {
  text-align: right;
}

/* 分页样式 */
.pagination-wrapper {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>
